package com.jsz.peini.controller.app;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.jsz.peini.bean.codes.SystemSeller;
import com.jsz.peini.bean.order.OrderInfo;
import com.jsz.peini.bean.order.OrderInfoExample;
import com.jsz.peini.bean.seller.SellerInfo;
import com.jsz.peini.bean.task.TaskInfo;
import com.jsz.peini.bean.task.TaskInfoExample;
import com.jsz.peini.bean.user.UserInfo;
import com.jsz.peini.bean.user.UserInfoExample;
import com.jsz.peini.common.bean.ResultMessage;
import com.jsz.peini.common.constants.Constants;
import com.jsz.peini.common.util.AppPush;
import com.jsz.peini.common.util.CommonUtils;
import com.jsz.peini.common.util.DateTimeUtils;
import com.jsz.peini.common.util.PeiniUtils;
import com.jsz.peini.common.util.ShieldTextFiltration;
import com.jsz.peini.common.util.SmsSendUtil;
import com.jsz.peini.common.util.SpringContextUtil;
import com.jsz.peini.common.util.ThreadUtil;
import com.jsz.peini.mapper.label.LabelUserMapper;
import com.jsz.peini.mapper.order.OrderInfoMapper;
import com.jsz.peini.mapper.order.OrderSellerMapper;
import com.jsz.peini.mapper.seller.SellerInfoMapper;
import com.jsz.peini.mapper.task.TaskCancelFlowMapper;
import com.jsz.peini.mapper.task.TaskInfoMapper;
import com.jsz.peini.mapper.user.UserInfoMapper;
import com.jsz.peini.mapper.user.UserSmsMapper;
import com.jsz.peini.redis.util.TaskRedisUtil;
import com.jsz.peini.redis.util.UserRedisUtil;
import com.jsz.peini.server.thread.TaskCancelTimerTask;
import com.jsz.peini.service.seller.SellerInfoService;
import com.jsz.peini.service.task.TaskInfoService;
import com.jsz.peini.service.user.UserInfoService;
import com.jsz.peini.vo.CommonTaskInfo;
import com.jsz.peini.vo.MergedCommonTaskInfo;
import com.jsz.peini.vo.TaskInMapSearchVo;
import com.jsz.peini.vo.UserVo;

@Controller
public class TaskController {
	private static final Logger logger = LoggerFactory.getLogger(TaskController.class);
	@Autowired
	private TaskInfoService taskInfoService;

	@Autowired
	private UserInfoService userInfoService;

	@Autowired
	private LabelUserMapper labelUserMapper;

	@Autowired
	private SellerInfoService sellerInfoService;

	@Resource
	private TaskInfoMapper taskInfoMapper;
	@Resource
	private UserInfoMapper userInfoMapper;
	@Resource
	private TaskCancelFlowMapper taskCancelFlowMapper;
	@Resource
	private SellerInfoMapper sellerInfoMapper;
	@Resource
	private OrderSellerMapper orderSellerMapper;
	@Resource
	private UserSmsMapper userSmsMapper;
	@Resource
	private OrderInfoMapper orderInfoMapper;
	
	//任务预定通知id
	private static final String SMSTaskFrom = "39779"; 
	/**
	 * 地图首页合并任务
	 *
	 */
	@RequestMapping(value = "/getTaskMapList")
	@ResponseBody
	public Object getTaskMapList(HttpServletRequest request,
			@RequestParam(value="xpoint") String xpoint,
			@RequestParam(value="ypoint") String ypoint,
			@RequestParam(value="distance",required=false,defaultValue="200") Integer distance,
			@RequestParam(value="sort",required=false) Integer sort,
			@RequestParam(value="otherSex",required=false) Integer otherSex,
			@RequestParam(value="otherLowAge",required=false) Integer otherLowAge,
			@RequestParam(value="otherHignAge",required=false) Integer otherHignAge,
			@RequestParam(value="otherLowheight",required=false) Integer otherLowheight,
			@RequestParam(value="otherHignheight",required=false) Integer otherHignheight,
			@RequestParam(value="isVideo",required=false) Integer isVideo,
			@RequestParam(value="isIdcard",required=false) Integer isIdcard,
			@RequestParam(value="sellerType",required=false) String sellerType,
			@RequestParam(value="taskCity",required=false,defaultValue="64") Integer taskCity
			) {
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		try {
			if(StringUtils.isBlank(xpoint) || StringUtils.isBlank(ypoint) || taskCity == null || distance == null){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "缺少参数");
				return infoMap;
			}
			
			TaskInfo taskInfo = new TaskInfo();
			taskInfo.setXpoint(xpoint);
			taskInfo.setYpoint(ypoint);
			taskInfo.setSort(sort);
			taskInfo.setOtherSex(otherSex);
			taskInfo.setOtherLowAge(otherLowAge);
			taskInfo.setOtherHignAge(otherHignAge);
			taskInfo.setOtherLowheight(otherLowheight);
			taskInfo.setOtherHignheight(otherHignheight);
			taskInfo.setIsVideo(isVideo);
			taskInfo.setIsIdcard(isIdcard);
			taskInfo.setSellerType(sellerType);
			taskInfo.setTaskCity(taskCity);
			
			if (StringUtils.isNotBlank(taskInfo.getSellerType())) {
				String[] arr = taskInfo.getSellerType().split(",");
				int[] arrInt = new int[arr.length];
				for (int i = 0; i < arr.length; i++) {
					arrInt[i] = Integer.parseInt(arr[i]);
				}
				taskInfo.setArr(arrInt);
			}
			
			//List<LinkedHashMap<String, Object>> taskAllList = taskInfoService.selectByALL(taskInfo);// 首先获取所有任务
			List<LinkedHashMap<String, Object>> taskAllList = TaskRedisUtil.getAll();
			List<LinkedHashMap<String, Object>> taskSearchList = new ArrayList<LinkedHashMap<String, Object>>();//过滤掉不符合筛选条件后的列表
			
			//根据筛选条件进行数据过滤
			for (LinkedHashMap<String, Object> taskMap : taskAllList) {
				if(taskInfo.getOtherSex() != null){
					if(!taskMap.get("sex").toString().equals(taskInfo.getOtherSex().toString())){
						continue;
					}
				}
				
				if(taskInfo.getOtherLowAge() != null && taskInfo.getOtherHignAge() != null){
					Integer age = Integer.parseInt(taskMap.get("age").toString());
					if(age < taskInfo.getOtherLowAge() || age > taskInfo.getOtherHignAge()){
						continue;
					}
				}
				
				if(taskInfo.getOtherLowheight() != null && taskInfo.getOtherHignheight() != null){
					Integer height = Integer.parseInt(taskMap.get("height").toString());
					if(height < taskInfo.getOtherLowheight() || height > taskInfo.getOtherHignheight()){
						continue;
					}
				}
				
				if(taskInfo.getIsVideo() != null && taskMap.get("isVideo") != null){
					if(!taskInfo.getIsVideo().toString().equals(taskMap.get("isVideo").toString())){
						continue;
					}
				}
				
				if(taskInfo.getIsIdcard() != null){
					if(!taskInfo.getIsIdcard().toString().equals(taskMap.get("isIdcard").toString())){
						continue;
					}
				}
				
				if(StringUtils.isNotEmpty(taskInfo.getSellerType())){
					if(sellerType.indexOf(",") != 0){
						sellerType = "," + sellerType;
					}
					if(sellerType.lastIndexOf(",") != sellerType.length()-1){
						sellerType += ",";
					}
					if(!sellerType.contains(","+taskMap.get("sellerType").toString()+",")){
						continue;
					}
				}
				
				if(taskInfo.getTaskCity() != null){
					if(!taskInfo.getTaskCity().toString().equals(taskMap.get("taskCity").toString())){
						continue;
					}
				}
				
				taskSearchList.add(taskMap);
			}
			
			List<LinkedHashMap<String, Object>> taskList = new ArrayList<LinkedHashMap<String, Object>>();// 存储筛选后的任务
			List<LinkedHashMap<String, Object>> taskOtherList = new ArrayList<LinkedHashMap<String, Object>>();// 存储可变的任务
			List<LinkedHashMap<String, Object>> taskResultList = new ArrayList<LinkedHashMap<String, Object>>();// 存储可变的任务
			
			if(taskSearchList.size() == 0){
				infoMap.put("taskMapList", taskResultList);
				infoMap.put("distance", 2000000);
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("resultDesc", "成功");
				return infoMap;
			}
			//distance = 200;
			int smalldistance = 0;
			
			boolean flag = true;
			while(flag == true){
				
				int bigdistance = distance * 10;// 显示的最大范围
				smalldistance = (int) (distance * 1.5);// 任务合并距离
				taskList.clear();
				taskOtherList.clear();
				
				// 筛选距离内的任务
				for (LinkedHashMap<String, Object> taskMap : taskSearchList) {
					
					// 如果距离大于当前
					if (CommonUtils.contrastMap(taskInfo.getXpoint(), taskInfo.getYpoint(), (String) taskMap.get("xpoint"),
							(String) taskMap.get("ypoint")) < bigdistance) {
						taskMap.put("status", 1);// 状态初始化赋予1
						taskList.add(taskMap);
						taskOtherList.add(taskMap);
					}
				}
				
				if(taskList.size() == 0){
					if(distance < 200 ){
						distance = 500;
					}else if(distance >= 200 && distance < 500){
						distance = 500;
					}else if(distance >= 500 && distance < 1000){
						distance = 1000;
					}else if(distance >= 1000 && distance < 2000){
						distance = 2000;
					}else if(distance >= 2000 && distance < 5000){
						distance = 5000;
					}else if(distance >= 5000 && distance < 10000){
						distance = 10000;
					}else if(distance >= 10000 && distance < 20000){
						distance = 20000;
					}else if(distance >= 20000 && distance < 25000){
						distance = 25000;
					}else if(distance >= 25000 && distance < 50000){
						distance = 50000;
					}else if(distance >= 50000 && distance < 100000){
						distance = 100000;
					}else if(distance >= 100000 && distance < 200000){
						distance = 200000;
					}else if(distance >= 200000 && distance < 500000){
						distance = 500000;
					}else if(distance >= 500000 && distance < 1000000){
						distance = 1000000;
					}else if(distance >= 1000000 && distance < 2000000){
						distance = 2000000;
					}else if(distance == 2000000){
						flag = false;
					}
				}else{
					flag = false;
				}
			}
			
			//Map<Integer, String> sellerMap = new HashMap<Integer, String>();

			//根据距离进行任务合并
			for (int i = 0; i < taskList.size(); i++) {
				if (taskOtherList.size() > 0 && (int) taskOtherList.get(i).get("status") == 1) {
					LinkedHashMap<String, Object> innerMap = new LinkedHashMap<String, Object>();
					List<LinkedHashMap<String, Object>> taskMapList = new ArrayList<LinkedHashMap<String, Object>>();// 增加的List
					int sum = 0;
					String idStr = "";
					
					for (int j = 0; j < taskOtherList.size(); j++) {
						if ((int) taskOtherList.get(j).get("status") == 1) {
							/*Integer sellerId = Integer.parseInt(taskOtherList.get(j).get("sellerId").toString());
							
							if(sellerMap.containsKey(sellerId)){
								taskOtherList.get(j).put("status", 0);
								taskMapList.add(taskOtherList.get(j));
								sum += 1;
								idStr += taskOtherList.get(j).get("id")+",";
							}else{
								sellerMap.put(sellerId, sellerId.toString());
								taskOtherList.get(j).put("status", 0);
								taskMapList.add(taskOtherList.get(j));
								sum = 1;
								idStr += taskOtherList.get(j).get("id")+",";
							}*/
							
							if (CommonUtils.contrastMap((String) taskList.get(i).get("xpoint"),
									(String) taskList.get(i).get("ypoint"), (String) taskOtherList.get(j).get("xpoint"),
									(String) taskOtherList.get(j).get("ypoint")) < smalldistance) {
								taskOtherList.get(j).put("status", 0);
								taskMapList.add(taskOtherList.get(j));
								sum += 1;
								idStr += taskOtherList.get(j).get("id")+",";
							}
						}
					}
					
					innerMap.put("id", taskMapList.get(0).get("id"));
					innerMap.put("sum", sum);
					innerMap.put("idStr", idStr);
					
					LinkedHashMap<String, Object> goldHigh = null;//金币榜最高名次
					LinkedHashMap<String, Object> payHigh = null;//土豪榜最高名次
					LinkedHashMap<String, Object> intHigh = null;//诚信榜最高名次
					LinkedHashMap<String, Object> repuHigh = null;//信誉最好
					
					//筛选要显示的任务
					int rankType = 0;//无皇冠；1金币；2土豪；3信誉
					for (LinkedHashMap<String, Object> taskMap : taskMapList) {
						if(goldHigh == null){
							String userId = taskMap.get("userId").toString();
							UserVo userVo = UserRedisUtil.getUserVoByUserId(userId);
							if(userVo.getGoldList() == null || userVo.getGoldList().equals("0")){
								
							}else{
								goldHigh = taskMap;
							}
							//logger.info(userVo.getUserId()+"--------gold:"+userVo.getGoldList());
						}else{
							String userId = taskMap.get("userId").toString();
							UserVo userVo = UserRedisUtil.getUserVoByUserId(userId);
							if(userVo.getGoldList() == null || userVo.getGoldList().equals("0")){
								
							}else{
								String highUser = goldHigh.get("userId").toString();
								UserVo highVo = UserRedisUtil.getUserVoByUserId(highUser);
								
								Long userHigh = 0l;
								if(userVo.getGoldList() != null && userVo.getGoldList().longValue() > 0){
									userHigh = userVo.getGoldList().longValue();
								}
								
								Long high = 0l;
								if(highVo.getGoldList() != null && highVo.getGoldList().longValue() > 0){
									high = highVo.getGoldList().longValue();
								}
								
								if(high == 0){
									goldHigh = taskMap;
									//logger.info(userVo.getUserId()+"--------gold:"+userVo.getGoldList());
								}else if(userHigh != 0 && userHigh < high){
									goldHigh = taskMap;
									//logger.info(userVo.getUserId()+"--------gold:"+userVo.getGoldList());
								}
							}
						}
						if(payHigh == null){
							String userId = taskMap.get("userId").toString();
							UserVo userVo = UserRedisUtil.getUserVoByUserId(userId);
							if("0".equals(userVo.getBuyList())){
								
							}else{
								payHigh = taskMap;
							}
							//logger.info(userVo.getUserId()+"--------pay:"+userVo.getBuyList());
						}else{
							String userId = taskMap.get("userId").toString();
							UserVo userVo = UserRedisUtil.getUserVoByUserId(userId);
							if(userVo.getBuyList() == null || "0".equals(userVo.getBuyList())){
								
							}else{
								String highUser = payHigh.get("userId").toString();
								UserVo highVo = UserRedisUtil.getUserVoByUserId(highUser);
								
								Long userHigh = 0l;
								if(userVo.getBuyList() != null && userVo.getBuyList().longValue() > 0){
									userHigh = userVo.getBuyList().longValue();
								}
								
								Long high = 0l;
								if(highVo.getBuyList() != null && highVo.getBuyList().longValue() > 0){
									high = highVo.getBuyList().longValue();
								}
								
								if(high == 0){
									payHigh = taskMap;
									//logger.info(userVo.getUserId()+"--------pay:"+userVo.getBuyList());
								}else if(userHigh != 0 && userHigh < high){
									payHigh = taskMap;
									//logger.info(userVo.getUserId()+"--------pay:"+userVo.getBuyList());
								}
							}
						}
						if(intHigh == null){
							String userId = taskMap.get("userId").toString();
							UserVo userVo = UserRedisUtil.getUserVoByUserId(userId);
							if("0".equals(userVo.getIntegrityList())){
								
							}else{
								intHigh = taskMap;
							}
						}else{
							String userId = taskMap.get("userId").toString();
							UserVo userVo = UserRedisUtil.getUserVoByUserId(userId);
							if("0".equals(userVo.getBuyList())){
								
							}else{
								String highUser = intHigh.get("userId").toString();
								UserVo highVo = UserRedisUtil.getUserVoByUserId(highUser);
								
								Long userHigh = 0l;
								if(userVo.getIntegrityList() != null && userVo.getIntegrityList().longValue() > 0){
									userHigh = userVo.getIntegrityList().longValue();
								}
								
								Long high = 0l;
								if(highVo.getIntegrityList() != null && highVo.getIntegrityList().longValue() > 0){
									high = highVo.getIntegrityList().longValue();
								}
								
								if(high == 0){
									intHigh = taskMap;
									//logger.info(userVo.getUserId()+"--------int:"+userVo.getIntegrityList());
								}else if(userHigh != 0 && userHigh < high){
									intHigh = taskMap;
									//logger.info(userVo.getUserId()+"--------int:"+userVo.getIntegrityList());
								}
							}
						}
						
						if(repuHigh == null){
							String userId = taskMap.get("userId").toString();
							UserVo userVo = UserRedisUtil.getUserVoByUserId(userId);
							if(userVo.getReputation().equals("0")){
								
							}else{
								repuHigh = taskMap;
							}
						}else{
							String userId = taskMap.get("userId").toString();
							UserVo userVo = UserRedisUtil.getUserVoByUserId(userId);
							if(!userVo.getReputation().equals("0")){
								String highUser = repuHigh.get("userId").toString();
								UserVo highVo = UserRedisUtil.getUserVoByUserId(highUser);
								
								if(userVo.getReputation().longValue() > highVo.getReputation().longValue()){
									repuHigh = taskMap;
								}
							}
						}
					}
					
					List<LinkedHashMap<String, Object>> taskShow = new ArrayList<LinkedHashMap<String, Object>>();// 存储要显示的任务
					if(goldHigh != null && payHigh != null && intHigh != null){
						
						int type = 0;
						String goldUser = goldHigh.get("userId").toString();
						UserVo goldVo = UserRedisUtil.getUserVoByUserId(goldUser);
						long goldSort = goldVo.getGoldList().longValue();
						
						String payUser = payHigh.get("userId").toString();
						UserVo payVo = UserRedisUtil.getUserVoByUserId(payUser);
						if(payVo.getBuyList() == null){
							UserInfo userInfo = userInfoMapper.selectByPrimaryKey(payVo.getUserId());
							payVo.setBuyList(new BigDecimal(userInfo.getBuyList()));
							UserRedisUtil.addUserToRedis(payVo);
						}
						long paySort = payVo.getBuyList().longValue();
						
						String intUser = payHigh.get("userId").toString();
						UserVo intVo = UserRedisUtil.getUserVoByUserId(intUser);
						long intSort = intVo.getIntegrityList().longValue();
						
						if(goldSort != 0 && paySort == 0 && intSort == 0){
							type = 1;
						}else if(goldSort == 0 && paySort != 0 && intSort == 0){
							type = 2;
						}else if(goldSort == 0 && paySort == 0 && intSort != 0){
							type = 3;
						}else if(goldSort != 0 && paySort != 0 && intSort == 0){
							if(goldSort < paySort){
								type = 1;
							}else{
								type = 2;
							}
						}else if(goldSort != 0 && paySort == 0 && intSort != 0){
							if(goldSort < intSort){
								type = 1;
							}else{
								type = 3;
							}
						}else if(goldSort == 0 && paySort != 0 && intSort != 0){
							if(paySort < intSort){
								type = 2;
							}else{
								type = 3;
							}
						}else if(goldSort != 0 && paySort != 0 && intSort != 0){
							if(goldSort < paySort){
								type = 1;
								if(goldSort < intSort){
									type = 1;
								}else{
									type = 3;
								}
							}else{
								type = 2;
								if(paySort < intSort){
									type = 2;
								}else{
									type = 3;
								}
							}
						}
						
						if(type == 1){
							rankType = 1;
							taskShow.add(goldHigh);
						}else if(type == 2){
							rankType = 2;
							taskShow.add(payHigh);
						}else if(type == 3){
							rankType = 3;
							taskShow.add(intHigh);
						}else{
							rankType = 0;
							taskShow.add(repuHigh);
						}
					}else if(repuHigh != null){
						taskShow.add(repuHigh);
					}else{
						taskShow.add(taskMapList.get(0));
					}
					
					innerMap.put("rankType", rankType);
					innerMap.put("taskObject", taskShow);
					taskResultList.add(innerMap);
				}
			}
			
			/*for (LinkedHashMap<String, Object> taskMap : taskList) {
				LinkedHashMap<String, Object> innerMap = new LinkedHashMap<String, Object>();
				
				List<LinkedHashMap<String, Object>> taskShow = new ArrayList<LinkedHashMap<String, Object>>();// 存储要显示的任务
				
				innerMap.put("id", taskMap.get("id"));
				innerMap.put("sum", 1);
				innerMap.put("idStr", taskMap.get("id").toString());
				
				String userId = taskMap.get("userId").toString();
				UserVo userVo = UserRedisUtil.getUserVoByUserId(userId);
				
				int type = 0;
				long goldSort = userVo.getGoldList().longValue();
				
				long paySort = userVo.getBuyList().longValue();
				
				long intSort = userVo.getIntegrityList().longValue();
				
				if(goldSort != 0 && paySort == 0 && intSort == 0){
					type = 1;
				}else if(goldSort == 0 && paySort != 0 && intSort == 0){
					type = 2;
				}else if(goldSort == 0 && paySort == 0 && intSort != 0){
					type = 3;
				}else if(goldSort != 0 && paySort != 0 && intSort == 0){
					if(goldSort < paySort){
						type = 1;
					}else{
						type = 2;
					}
				}else if(goldSort != 0 && paySort == 0 && intSort != 0){
					if(goldSort < intSort){
						type = 1;
					}else{
						type = 3;
					}
				}else if(goldSort == 0 && paySort != 0 && intSort != 0){
					if(paySort < intSort){
						type = 2;
					}else{
						type = 3;
					}
				}else if(goldSort != 0 && paySort != 0 && intSort != 0){
					if(goldSort < paySort){
						type = 1;
						if(goldSort < intSort){
							type = 1;
						}else{
							type = 3;
						}
					}else{
						type = 2;
						if(paySort < intSort){
							type = 2;
						}else{
							type = 3;
						}
					}
				}
				innerMap.put("rankType", type);
				
				taskShow.add(taskMap);
				innerMap.put("taskObject", taskShow);
				taskResultList.add(innerMap);
			}*/
			
			for (LinkedHashMap<String, Object> linkedHashMap : taskResultList) {
				List<LinkedHashMap<String, Object>> tshow = (List<LinkedHashMap<String, Object>>)linkedHashMap.get("taskObject");
				for (int i = 0; i < tshow.size(); i++) {
					tshow.set(i, getShowTask(tshow.get(i)));
					tshow.get(i).put("sellerType", PeiniUtils.yetaiMap.get(tshow.get(i).get("sellerType").toString()).getCodesOrder());
					String userId = tshow.get(i).get("userId").toString();
					UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
					if(userInfo == null){
						tshow.get(i).put("imageHead","");
					}else{
						tshow.get(i).put("imageHead",StringUtils.trimToEmpty(userInfo.getImageHead()));
					}
					//logger.info(userInfo.getImageHead());
					//logger.info(tshow.get(i).get("sellerType"));
				}
				linkedHashMap.put("taskObject", tshow);
			}

			infoMap.put("taskMapList", taskResultList);
			infoMap.put("distance", distance);
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", "成功");
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		//logger.info(net.sf.json.JSONObject.fromObject(infoMap));
		return infoMap;
	}
	
	/**
	 * 去除地图上不需要展示的属性
	 * @author Han Guohong
	 * @param taskInfo
	 * @return
	 */
	private LinkedHashMap<String, Object> getShowTask(LinkedHashMap<String, Object> taskInfo){
		taskInfo.remove("gold_list");
		taskInfo.remove("buy_list");
		taskInfo.remove("integrity_list");
		taskInfo.remove("reputation");
		taskInfo.remove("taskCity");
		taskInfo.remove("age");
		taskInfo.remove("height");
		taskInfo.remove("isVideo");
		taskInfo.remove("isIdcard");
		taskInfo.remove("isDelete");
		//taskInfo.remove("status");
		taskInfo.remove("taskAppintedTime");
		taskInfo.remove("seller_small_type");
		return taskInfo;
	}

	private static DateFormat jszDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * 任务列表信息获取及筛选
	 * 
	 * @param request
	 * @param distance
	 * @param taskInfo
	 * @return
	 */
	@RequestMapping(value = "/selectTaskInfoBySort")
	@ResponseBody
	public Object selectTaskInfoBySort(HttpServletRequest request,
			@RequestParam(value="xpoint") String xpoint,
			@RequestParam(value="ypoint") String ypoint,
			@RequestParam(value="sort",required=false,defaultValue="1") Integer sort,
			@RequestParam(value="otherSex",required=false) Integer otherSex,
			@RequestParam(value="otherLowAge",required=false) Integer otherLowAge,
			@RequestParam(value="otherHignAge",required=false) Integer otherHignAge,
			@RequestParam(value="otherLowheight",required=false) Integer otherLowheight,
			@RequestParam(value="otherHignheight",required=false) Integer otherHignheight,
			@RequestParam(value="isVideo",required=false) Integer isVideo,
			@RequestParam(value="isIdcard",required=false) Integer isIdcard,
			@RequestParam(value="sellerType",required=false) String sellerType,
			@RequestParam(value="taskCity",required=false,defaultValue="64") Integer taskCity,
			//TaskInfo taskInfo,
			@RequestParam(value="idStr",required=false) String idStr
			) {
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		try {
			if(StringUtils.isBlank(xpoint) || StringUtils.isBlank(ypoint)){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
				return infoMap;
			}
			
			String userId = request.getParameter("userId");
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			userId = obj.getString("userId");
			
			TaskInfo taskInfo = new TaskInfo();
			taskInfo.setXpoint(xpoint);
			taskInfo.setYpoint(ypoint);
			taskInfo.setSort(sort);
			taskInfo.setOtherSex(otherSex);
			taskInfo.setOtherLowAge(otherLowAge);
			taskInfo.setOtherHignAge(otherHignAge);
			taskInfo.setOtherLowheight(otherLowheight);
			taskInfo.setOtherHignheight(otherHignheight);
			taskInfo.setIsVideo(isVideo);
			taskInfo.setIsIdcard(isIdcard);
			taskInfo.setSellerType(sellerType);
			taskInfo.setTaskCity(taskCity);
			
			if (StringUtils.isNotBlank(taskInfo.getSellerType())) {
				String[] arr = taskInfo.getSellerType().split(",");
				int[] arrInt = new int[arr.length];
				for (int i = 0; i < arr.length; i++) {
					arrInt[i] = Integer.parseInt(arr[i]);
				}
				taskInfo.setArr(arrInt);
			}
			
			if(StringUtils.isNotBlank(idStr)){
				String [] arr = idStr.split(",");
				int[] ids = new int[arr.length];
				for (int i = 0; i < ids.length; i++) {
					if(StringUtils.isNotBlank(arr[i])){
						ids[i] = Integer.parseInt(arr[i]);
					}
				}
				taskInfo.setTaskIds(ids);
			}
			
			List<LinkedHashMap<String, Object>> taskAllList = taskInfoService.selectTaskInfoBySort(taskInfo);// 首先获取所有任务
			for(Map<String, Object> map : taskAllList){
				map.put("weekDayOfAppointedTime", jszDateFormat.parse((String)map.get("taskAppointedTime")).getDay());
				map.put("credit", "高");
				map.put("zmPoint", "801");
			}
			
			for (int i = 0; i < taskAllList.size(); i++) {
				List<LinkedHashMap<String, Object>> userLabel = labelUserMapper
						.selectLabelUserByUserInfoId((String) taskAllList.get(i).get("userId"));
				if (userLabel.size() > 3) {
					taskAllList.get(i).put("userLabel", userLabel.subList(0, 3));
				} else {
					taskAllList.get(i).put("userLabel", userLabel);
				}
				String sellerSmallName = PeiniUtils.yetaiMap.get(taskAllList.get(i).get("sellerSmallType").toString()).getCodesContent();
				taskAllList.get(i).put("sellerSmallName", sellerSmallName == null ? "" : sellerSmallName);
				
				if(taskAllList.get(i).get("userId").toString().equals(userId)){//我发布的
					taskAllList.get(i).put("publishType", 1);
				}else{//他发布的
					taskAllList.get(i).put("publishType", 0);
				}
			}
			for (int i = 0; i < taskAllList.size(); i++) {
				int distanceNum = CommonUtils.contrastMap(taskInfo.getXpoint(), taskInfo.getYpoint(),
						(String) taskAllList.get(i).get("xpoint"), (String) taskAllList.get(i).get("ypoint"));
				taskAllList.get(i).put("distanceNum", distanceNum);
				taskAllList.get(i).put("othernickName",PeiniUtils.updateNickName(taskAllList.get(i).get("othernickName").toString(), taskAllList.get(i).get("otheraccCode").toString()));
			}
			if (taskInfo.getSort() != null && taskInfo.getSort() == 1) {
				infoMap.put("taskAllList", taskAllList);
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("resultDesc", "成功");
			}
			if (taskInfo.getSort() != null && taskInfo.getSort() == 2) {
				Collections.sort(taskAllList, new Comparator<LinkedHashMap<String, Object>>() {
					@Override
					public int compare(LinkedHashMap<String, Object> o1, LinkedHashMap<String, Object> o2) {
						if (Integer.parseInt(o1.get("distanceNum").toString()) < Integer
								.parseInt(o2.get("distanceNum").toString())) {
							return 1;
						}
						if (o1.get("distanceNum") == o2.get("distanceNum")) {
							return 0;
						}
						return -1;
					}
				});
				infoMap.put("taskAllList", taskAllList);
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("resultDesc", "成功");
			}else if(taskInfo.getSort() != null && taskInfo.getSort() == 1){
				infoMap.put("taskAllList", taskAllList);
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("resultDesc", "成功");
			}
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		//logger.info(net.sf.json.JSONObject.fromObject(infoMap));
		return infoMap;
	}

	/**
	 * 任务信息发布
	 * 
	 * @param request
	 * @param taskInfo
	 * @return
	 */
	@RequestMapping(value = "/setTaskInfo")
	@ResponseBody
	public Object setTaskInfo(HttpServletRequest request,
			@RequestParam(value="userId") String userId,
			@RequestParam(value="taskProvince") Integer taskProvince,
			@RequestParam(value="taskCity") Integer taskCity,
			@RequestParam(value="sellerInfoId") Integer sellerInfoId,
			@RequestParam(value="taskAppointedTime",required=false) String taskAppointedTime,
			@RequestParam(value="taskDesc",required=false) String taskDesc,
			@RequestParam(value="otherSex",required=false) Integer otherSex,//1男；2女；3不限
			@RequestParam(value="otherLowAge",required=false) Integer otherLowAge,//如果年龄不限为空
			@RequestParam(value="otherHignAge",required=false) Integer otherHignAge,//如果年龄不限为空
			@RequestParam(value="otherBuy",required=false) Integer otherBuy,
			@RequestParam(value="otherGo",required=false) Integer otherGo,
			@RequestParam(value="xpoint",required=false) String xpoint,
			@RequestParam(value="ypoint",required=false) String ypoint
			) {
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		infoMap.put("isHead", 2);
		try {
			String newtaskDesc=ShieldTextFiltration.filter(taskDesc);
			if (!newtaskDesc.equals(taskDesc)) {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "您发布的任务描述涉及敏感字词请重新发送!");
				return infoMap;
			}
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			userId = obj.getString("userId");
			
			UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
			if(!PeiniUtils.isAddUserInfo(userInfo)){
				infoMap.put("resultCode", Constants.INFO_INVALIDATE);
				infoMap.put("resultDesc", "必要资料不全，请完善资料");
				return infoMap;
			}
			
			if(userInfo.getImgheadIsdelete() == null){
				userInfo.setImgheadIsdelete(0);
			}
			if (userInfo.getImgheadIsdelete()==1) {
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("isHead", Constants.SUCCESS);
				infoMap.put("resultDesc", "头像审核失败，非常抱歉，由于没有使用可看清脸的真实照片作为头像，您暂时无法进行任务操作，请尽快更换照片，得到更多人的喜欢吧~");
				return infoMap;
			}else if (userInfo.getImgheadIsdelete()==0) {
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("isHead", Constants.SUCCESS);
				infoMap.put("resultDesc", "头像正在审核中，您还无法体验任务互动功能~");
				return infoMap;
			}
			Date pointTime = DateTimeUtils.formatString2Date(taskAppointedTime, "yyyy-MM-dd HH:mm");
			if(pointTime.before(new Date())){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "不能选择当前时间之前的时间!");
				return infoMap;
			}
			
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("userId", userId);
			paramMap.put("taskTime", taskAppointedTime);
			int cnt = taskInfoMapper.selectCntByBetweenTime(paramMap);
			if(cnt > 0){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "任务选择时间点的上下1小时只能发布一条任务");
				return infoMap;
			}
			
			paramMap.clear();
			paramMap.put("userId", userId);
			cnt = taskInfoMapper.selectTodayCnt(paramMap);
			
			if(cnt >= 3){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "最多只能有三个任务同时在线");
				return infoMap;
			}
			
			infoMap = taskInfoService.setTaskInfo(request, userId, taskProvince, taskCity, sellerInfoId, taskAppointedTime,taskDesc, otherSex, otherLowAge, otherHignAge, otherBuy, otherGo, xpoint, ypoint);
			infoMap.put("isHead", 2);
			
			/*TaskInfo taskInfo = new TaskInfo();
			taskInfo.setUserId(userId);
			taskInfo.setTaskProvince(taskProvince);
			taskInfo.setTaskCity(taskCity);
			taskInfo.setSellerInfoId(sellerInfoId);			
			
			SellerDiscountExample disExample = new SellerDiscountExample();
			disExample.createCriteria().andSellerInfoIdEqualTo(sellerInfoId);
			List<SellerDiscount> list = SellerDiscountMapper
			
			SellerInfo sellerInfo = sellerInfoMapper.selectByPrimaryKey(sellerInfoId);
			taskInfo.setSellerInfoName(sellerInfo.getSellerName());
			taskInfo.setSellerBigType(sellerInfo.getSellerType());
			taskInfo.setSellerSmallType(Integer.parseInt(sellerInfo.getLabelsId()));
			taskInfo.setTaskTime(new Date());
			taskInfo.setTaskStatus(1);
			
			taskInfo.setTaskAppointedTime(pointTime);
			taskInfo.setTaskDesc(taskDesc);
			taskInfo.setOtherSex(otherSex);
			taskInfo.setOtherLowAge(otherLowAge);
			taskInfo.setOtherHignAge(otherHignAge);
			taskInfo.setOtherBuy(otherBuy);
			taskInfo.setOtherGo(otherGo);
			taskInfo.setXpoint(sellerInfo.getXpoint() == null ? "0" : sellerInfo.getXpoint());
			taskInfo.setYpoint(sellerInfo.getYpoint() == null ? "0" : sellerInfo.getYpoint());
			
			paramMap.clear();
			paramMap.put("userId", userId);
			
			if (taskInfo != null) {
				int setTaskInfoNum = taskInfoMapper.insertSelective(taskInfo);
				if (setTaskInfoNum > 0) {
					
					//将任务写入redis缓存
					List<LinkedHashMap<String, Object>> taskList = taskInfoMapper.selectByALL(taskInfo);
					for (LinkedHashMap<String, Object> taskMap : taskList) {
						TaskRedisUtil.addTaskToRedis(taskMap);
					}
					
					//userInfoService.updateUserInfoTaskNumByPlusAndMinus(taskInfo.getUserId(), +3);
					//userInfoService.updateCredit(taskInfo.getUserId());
					int taskNum = userInfoService.getTaskNum(taskInfo.getUserId(), +3);
					UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
					userInfo.setTaskNum(taskNum);
					userInfo.setReputation(PeiniUtils.getCredit(userInfo));
					userInfoMapper.updateByPrimaryKey(userInfo);
				}
			}
			
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", "任务发布成功");*/
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}

	/**
	 * 任务（详情）接口
	 * 
	 * @param request
	 * @param taskInfo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getTaskInfo",produces="text/plain;charset=UTF-8")
	public String getTaskInfo(HttpServletRequest request, /*TaskInfo taskInfo,*/ String xpointMe, String ypointMe,
				@RequestParam(value="id") Integer id,
				@RequestParam(value="userId") String userId,
				@RequestParam(value="type",required=false) Integer type,//1通知；不传为任务详情
				@RequestParam(value="orderId",required=false) Long orderId
			) {
		net.sf.json.JSONObject ooObject = new net.sf.json.JSONObject();
		try {
			
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj.toJSONString();
			}
			userId = obj.getString("userId");
			
			UserInfo uInfo = userInfoMapper.selectByPrimaryKey(userId);
			if(!PeiniUtils.isAddUserInfo(uInfo)){
				obj.put("resultCode", Constants.INFO_INVALIDATE);
				obj.put("resultDesc", "必要资料不全，请完善资料");
				return obj.toJSONString();
			}
			
			int linkType = 1;//任务详情
			TaskInfo taskInfo = taskInfoMapper.selectByPrimaryKey(id);
			
			if(type == null){
				linkType = 1;
			}else{
				OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(orderId);
				if(orderInfo.getOrderStatus() == 1){//跳转评价
					linkType = 2;
				}else if(orderInfo.getOrderStatus() == 2){//任务详情
					linkType = 1;
				}
			}
			
			SellerInfo sellerInfo = sellerInfoMapper.selectByPrimaryKey(taskInfo.getSellerInfoId());
			
			if(linkType == 1){
			
				if(taskInfo.getTaskStatus() >= 2){
					if(!taskInfo.getUserId().equals(userId) && taskInfo.getOtherUserId() != null && !taskInfo.getOtherUserId().equals(userId)){
						/*infoMap.put("resultCode", Constants.FAIL);
						infoMap.put("resultDesc", "该任务已经有人接单了");
						return infoMap;*/
						taskInfo.setTaskStatus(1);
						taskInfo.setTaskOtherStatus(null);
					}
				}
				
				
				UserInfo userInfo = userInfoMapper.selectByPrimaryKey(taskInfo.getUserId());
				
				taskInfo.setSellerInfoName(sellerInfo.getSellerName());
				taskInfo.setUserPhone(userInfo.getUserLogin().getUserName());
				taskInfo.setNickName(PeiniUtils.updateNickName(userInfo.getNickname(),userInfo.getAccCode()));
				taskInfo.setImageHead(userInfo.getImageHead());
				taskInfo.setSex(userInfo.getSex() == null ? 3 : userInfo.getSex());
				taskInfo.setAge(userInfo.getAge());
				taskInfo.setGoldList(userInfo.getGoldList());
				taskInfo.setBuyList(userInfo.getBuyList());
				taskInfo.setIntegrityList(userInfo.getIntegrityList());
				taskInfo.setReputation(userInfo.getReputation());
				taskInfo.setImageHead(StringUtils.isEmpty(userInfo.getImageHead()) ? "" : userInfo.getImageHead() );
				
				String sellerSmallName = PeiniUtils.yetaiMap.get(taskInfo.getSellerSmallType().toString()).getCodesContent();
				taskInfo.setSellerSmallName(sellerSmallName);
				
				if(taskInfo.getTaskStatus() > 1){
					if(StringUtils.isNotBlank(taskInfo.getOtherUserId())){
						UserInfo otherInfo = userInfoMapper.selectByPrimaryKey(taskInfo.getOtherUserId());
						if(otherInfo != null){
							taskInfo.setOtherUserPhone(otherInfo.getUserPhone());
							taskInfo.setOthernickName(PeiniUtils.updateNickName(otherInfo.getNickname(),otherInfo.getAccCode()));
							taskInfo.setOtherUserSex(otherInfo.getSex() == null ? 3 : otherInfo.getSex());
							taskInfo.setOtherGoldList(otherInfo.getGoldList());
							taskInfo.setOtherBuyList(otherInfo.getBuyList());
							taskInfo.setOtherIntegrityList(otherInfo.getIntegrityList());
							taskInfo.setOtherReputation(otherInfo.getReputation());
							taskInfo.setSignWord(otherInfo.getSignWord());
						}else{
							taskInfo.setOtherUserPhone("");
							taskInfo.setOthernickName("");
							taskInfo.setOtherUserSex(3);
							taskInfo.setOtherGoldList(0);
							taskInfo.setOtherBuyList(0);
							taskInfo.setOtherIntegrityList(0);
							taskInfo.setOtherReputation(0);
							taskInfo.setSignWord("");
						}
					}
				}
				
				int distance = CommonUtils.contrastMap(xpointMe, ypointMe,
						(String) taskInfo.getXpoint(), (String) taskInfo.getYpoint());
				
				taskInfo.setDistance(distance);
				
				List<LinkedHashMap<String, Object>> sellerImage = new ArrayList<LinkedHashMap<String, Object>>();
				/*int sellerId = taskInfo.getSellerInfoId();
				SellerInfo sellerInfo = sellerInfoMapper.selectByPrimaryKey(sellerId);*/
				String sellerImgs = sellerInfo.getSellerImg();
				if(StringUtils.isNotBlank(sellerImgs)){
					String [] imgs = sellerImgs.split(",");
					for (int i = 0; i < imgs.length; i++) {
						if(StringUtils.isNotBlank(imgs[i])){
							LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
							map.put("id", i+1);
							map.put("imageSrc", imgs[i]);
							sellerImage.add(map);
						}
					}
				}
				
				//List<LinkedHashMap<String, Object>> sellerImage = sellerImageService.selectSellerImageBySellerInfoId(taskInfo.getSellerInfoId());			
				taskInfo.setSellerImage(sellerImage);
				
				List<LinkedHashMap<String, Object>> userLabel = labelUserMapper.selectLabelUserByUserInfoId((String) taskInfo.getUserId());
				taskInfo.setUserLabel(userLabel);
				
				if(StringUtils.isNotBlank(taskInfo.getOtherUserId())){
					List<LinkedHashMap<String, Object>> otherUserLabel = labelUserMapper
							.selectLabelUserByUserInfoId((String) taskInfo.getOtherUserId());
					taskInfo.setOtherUserLabel(otherUserLabel);
				}
				
				net.sf.json.JSONObject object = net.sf.json.JSONObject.fromObject(taskInfo);
				object.put("taskAppointedTime", DateTimeUtils.formatDateTime2String(taskInfo.getTaskAppointedTime(), "yyyy-MM-dd HH:mm"));
				object.put("taskCancleTime", DateTimeUtils.formatDateTime2String(taskInfo.getTaskCancleTime(), "yyyy-MM-dd HH:mm:dd"));
				object.put("taskRecvTime", DateTimeUtils.formatDateTime2String(taskInfo.getTaskRecvTime(), "yyyy-MM-dd HH:mm:dd"));
				object.put("taskTime", DateTimeUtils.formatDateTime2String(taskInfo.getTaskTime(), "yyyy-MM-dd HH:mm:dd"));
				
				if(userId.equals(taskInfo.getUserId())){//我发布的
					object.put("publishType", 1);
				}else{//他人发布的
					object.put("publishType", 0);
				}
				
				OrderInfoExample example = new OrderInfoExample();
				com.jsz.peini.bean.order.OrderInfoExample.Criteria criteria = example.createCriteria();
				criteria.andOrderTypeEqualTo(1);
				if(taskInfo.getOtherBuy() == 1){
					criteria.andUserIdEqualTo(taskInfo.getUserId());
				}else if(taskInfo.getOtherBuy() == 2){
					if(StringUtils.isNotBlank(taskInfo.getOtherUserId())){
						criteria.andUserIdEqualTo(taskInfo.getOtherUserId());
					}
				}else if(taskInfo.getOtherBuy() == 3){
					criteria.andUserIdEqualTo(userId);
				}
				criteria.andTaskInfoIdEqualTo(taskInfo.getId());
				List<OrderInfo> orderList = orderInfoMapper.selectByExample(example);
				if(orderList.size() == 0){
					object.put("orderId", "");
				}else{
					object.put("orderId", orderList.get(0).getId());
				}
				
				object.put("taskStatus", taskInfo.getTaskStatus());
				object.put("taskOtherStatus", taskInfo.getTaskOtherStatus() == null ? "" : taskInfo.getTaskOtherStatus().toString());
				
				SystemSeller systemSeller = PeiniUtils.yetaiMap.get(taskInfo.getSellerBigType().toString());
				String imgSrc = systemSeller.getImageSrc();
				object.put("sellerTypeImg", imgSrc);
				
				String buyType = "";
				switch (taskInfo.getOtherBuy()) {
				case 1:
					buyType = "发起人买单";
					break;
				case 2:
					buyType = "接收人买单";
					break;
				case 3:
					buyType = "AA制买单";
					break;
				default:
					break;
				}
				object.put("shareTitle", PeiniUtils.updateNickName(userInfo.getNickname(), userInfo.getAccCode())+"在陪你发布了一个"+taskInfo.getSellerInfoName()+"任务，（"+buyType+"）点进去看看>>>");
				object.put("shareContent", "据说"+sellerInfo.getSellerName()+"口碑非常棒，一起去体验吧~");
				
				net.sf.json.JSONObject object2 = new net.sf.json.JSONObject();
				JSONArray array = new JSONArray();
				array.add(object);
				
				object2.put("taskInfoList", array);
				object2.put("resultCode", Constants.SUCCESS);
				object2.put("resultDesc", "成功");
				//logger.info(object2.toString());
				return object2.toString();
			}else{
				net.sf.json.JSONObject object = new net.sf.json.JSONObject();
				object.put("id", id);
				object.put("sellerInfoId", sellerInfo.getId());
				object.put("sellerInfoName", sellerInfo.getSellerName());
				
				UserInfo otherInfo = new UserInfo();
				if(taskInfo.getUserId().equals(userId)){//发布人
					otherInfo = userInfoMapper.selectByPrimaryKey(taskInfo.getOtherUserId());
				}else{
					otherInfo = userInfoMapper.selectByPrimaryKey(taskInfo.getUserId());
				}
				
				object.put("otherUserId", otherInfo.getId());
				object.put("othernickName", PeiniUtils.updateNickName(otherInfo.getNickname(), otherInfo.getAccCode()));
				object.put("orderId", orderId);
				
				net.sf.json.JSONObject object2 = new net.sf.json.JSONObject();
				JSONArray array = new JSONArray();
				array.add(object);
				
				object2.put("taskInfoList", array);
				object2.put("resultCode", Constants.SUCCESS_TASK);
				object2.put("resultDesc", "成功");
				//logger.info(net.sf.json.JSONObject.fromObject(infoMap));
				return object2.toString();
			}
			
		} catch (Exception e) {
			ooObject.put("resultCode", Constants.FAIL);
			ooObject.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		//logger.info(net.sf.json.JSONObject.fromObject(infoMap));
		return ooObject.toString();
	}

	/**
	 * 参加任务
	 * 
	 * @param request
	 * @param taskInfo
	 * @return
	 */
	@RequestMapping(value = "/updateTaskInfo")
	@ResponseBody
	public Object regTaskInfo(HttpServletRequest request, TaskInfo taskInfo) {
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		try {
			JSONObject obj = PeiniUtils.validToken(taskInfo.getOtherUserId());
			infoMap.put("isHead",2);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			String userId = obj.getString("userId");
			UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
			
			taskInfo.setOtherUserId(userId);
			
			taskInfo = taskInfoMapper.selectByPrimaryKey(taskInfo.getId());
			
			if(taskInfo.getUserId().equals(userId)){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "不允许参加自己的任务");
				return infoMap;
			}
			
			if(!PeiniUtils.isAddUserInfo(userInfo)){
				infoMap.put("resultCode", Constants.INFO_INVALIDATE);
				infoMap.put("resultDesc", "必要资料不全，请完善资料");
				return infoMap;
			}
			
			//UserInfo userInfoList=userInfoMapper.selectUserInfoByUserId(userId);
			if(userInfo.getImgheadIsdelete() == null){
				userInfo.setImgheadIsdelete(0);
			}
			if (userInfo.getImgheadIsdelete()==1) {
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("isHead", Constants.SUCCESS);
				infoMap.put("resultDesc", "头像审核失败，非常抱歉，由于没有使用可看清脸的真实照片作为头像，您暂时无法进行任务操作，请尽快更换照片，得到更多人的喜欢吧~");
				return infoMap;
			}else if (userInfo.getImgheadIsdelete()==0) {
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("isHead", 0);
				infoMap.put("resultDesc", "头像正在审核中，您还无法体验任务互动功能~");
				return infoMap;
			}
			
			UserInfo otherInfo = userInfoMapper.selectByPrimaryKey(userId);
			
			if(taskInfo.getTaskStatus() != 1){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "这个任务已经被人抢走了！");
				return infoMap;
			}
			
			boolean torf = false;
			if (taskInfo.getOtherLowAge() == null && taskInfo.getOtherHignAge() == null) {
				if (taskInfo.getOtherSex() != 3) {
					if (taskInfo.getOtherSex() == otherInfo.getSex()) {
						torf = true;
					}else{
						infoMap.put("resultCode", Constants.FAIL);
						infoMap.put("resultDesc", "您的性别不符合约会要求");
						return infoMap;
					}
				} else {
					torf = true;
				}
			} else if (taskInfo.getOtherLowAge() != null && taskInfo.getOtherHignAge() != null && taskInfo.getOtherLowAge() <= otherInfo.getAge()
					&& taskInfo.getOtherHignAge() >= otherInfo.getAge()) {
				//torf = true;
				if (taskInfo.getOtherSex() != 3) {
					if (taskInfo.getOtherSex() == otherInfo.getSex()) {
						torf = true;
					}else{
						infoMap.put("resultCode", Constants.FAIL);
						infoMap.put("resultDesc", "您的性别不符合约会要求");
						return infoMap;
					}
				} else {
					torf = true;
				}
			}else{
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "您的年龄不符合约会要求");
				return infoMap;
			}
			
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			
			paramMap.put("userId", userId);
			paramMap.put("taskTime", taskInfo.getTaskAppointedTime());
			int cnt = taskInfoMapper.selectCntByBetweenTime(paramMap);
			if(cnt > 0){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "任务时间点的上下1小时只能有一条任务在线");
				return infoMap;
			}
			
			if (torf) {
				switch (taskInfo.getOtherBuy()) {
				case 1:
					taskInfo.setTaskStatus(2);
					break;
				case 2:
					taskInfo.setTaskOtherStatus(21);
					break;
				case 3:
					taskInfo.setTaskStatus(2);
					taskInfo.setTaskOtherStatus(21);
					break;
				default:
					break;
				}
				
				taskInfo.setTaskStatus(2);
				taskInfo.setTaskOtherStatus(21);
				
				taskInfo.setOtherUserId(userId);
				Date date = new Date();
				taskInfo.setTaskRecvTime(date);
				int num=taskInfoService.updateTaskInfo(taskInfo,otherInfo,userId);
				if (num>0) {
					infoMap.put("resultCode", Constants.SUCCESS);
					infoMap.put("resultDesc", "成功");
					TaskCancelTimerTask taskCancelTimerTask = new TaskCancelTimerTask();
					taskCancelTimerTask.setTaskId(taskInfo.getId());
					
					Timer timer = new Timer();
					Calendar calendar = Calendar.getInstance();
					calendar.setTime(taskInfo.getTaskAppointedTime());
					calendar.add(Calendar.DATE, 1);
					//calendar.add(Calendar.MINUTE, 3);
					timer.schedule(taskCancelTimerTask, calendar.getTime());
					
					final Integer taskTimerId = taskInfo.getId();
					Date remindDate = new Date(taskInfo.getTaskAppointedTime().getTime()-1000*60*60*3);
					//Date remindDate = new Date(taskInfo.getTaskAppointedTime().getTime()-1000*60);
					if(remindDate.after(new Date())){
						new java.util.Timer().schedule(new TimerTask() {							
							@Override
							public void run() {
								TaskInfoMapper tInfoMapper = (TaskInfoMapper)SpringContextUtil.getBean(TaskInfoMapper.class);
								UserInfoMapper uInfoMapper = (UserInfoMapper)SpringContextUtil.getBean(UserInfoMapper.class);
								TaskInfo taskInfo = tInfoMapper.selectByPrimaryKey(taskTimerId);
								
								JSONObject object = new JSONObject();
						        object.put("title", "任务倒计时提醒");
						        object.put("type", 116);
						        object.put("paramId", taskTimerId);
						        object.put("content", "距“"+taskInfo.getSellerInfoName()+"”任务开启时间还有3小时，准时赴约可增加任务成功率哦~");
						        
						        
						        UserInfo uInfo = uInfoMapper.selectByPrimaryKey(taskInfo.getUserId());
						        UserInfo oInfo = uInfoMapper.selectByPrimaryKey(taskInfo.getOtherUserId());
						        
								AppPush.sendPush(object,uInfo.getUserPhone().split(","));
								AppPush.sendPush(object,oInfo.getUserPhone().split(","));
								
								SmsSendUtil.sendSmsNotify(uInfo.getId(), taskTimerId.toString(), 116, "距“"+taskInfo.getSellerInfoName()+"”任务开启时间还有3小时，准时赴约可增加任务成功率哦~");
								SmsSendUtil.sendSmsNotify(oInfo.getId(), taskTimerId.toString(), 116, "距“"+taskInfo.getSellerInfoName()+"”任务开启时间还有3小时，准时赴约可增加任务成功率哦~");
							}
						}, remindDate);
					}else{
						JSONObject object = new JSONObject();
				        object.put("title", "任务倒计时提醒");
				        object.put("type", 116);
				        object.put("paramId", taskTimerId);
				        object.put("content", "距“"+taskInfo.getSellerInfoName()+"”任务开启时间已不足3小时，准时赴约可增加任务成功率哦~");
				        
				        /*TaskInfoMapper tInfoMapper = (TaskInfoMapper)SpringContextUtil.getBean(TaskInfoMapper.class);
				        UserInfoMapper uInfoMapper = (UserInfoMapper)SpringContextUtil.getBean(UserInfoMapper.class);
				        TaskInfo taskInfo = tInfoMapper.selectByPrimaryKey(taskTimerId);*/
				        
				        UserInfo uInfo = userInfoMapper.selectByPrimaryKey(taskInfo.getUserId());
				        UserInfo oInfo = userInfoMapper.selectByPrimaryKey(taskInfo.getOtherUserId());
				        
						AppPush.sendPush(object,uInfo.getUserPhone().split(","));
						AppPush.sendPush(object,oInfo.getUserPhone().split(","));
						
						SmsSendUtil.sendSmsNotify(uInfo.getId(), taskTimerId.toString(), 116, "距“"+taskInfo.getSellerInfoName()+"”任务开启时间已不足3小时，准时赴约可增加任务成功率哦~");
						SmsSendUtil.sendSmsNotify(oInfo.getId(), taskTimerId.toString(), 116, "距“"+taskInfo.getSellerInfoName()+"”任务开启时间已不足3小时，准时赴约可增加任务成功率哦~");
					}
				}else {
					infoMap.put("resultCode", Constants.FAIL);
					infoMap.put("resultDesc", "网络请求失败");
				}
			} else {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "任务参加失败");
			}
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", "成功");
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	/**
	 * 关闭任务接口
	 * @author Han Guohong
	 * @param request
	 * @param userId
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value = "/delTaskInfo")
	@ResponseBody
	public Object delTaskInfo(HttpServletRequest request, 
			@RequestParam(value="userId") String userId,
			@RequestParam(value="taskId") Long taskId
			) {
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		try {
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			userId = obj.getString("userId");
			
			TaskInfo taskInfo = taskInfoMapper.selectByPrimaryKey(taskId.intValue());
			
			if(!taskInfo.getUserId().equals(userId) && !taskInfo.getOtherUserId().equals(userId)){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
				return infoMap;
			}
			
			if(taskInfo.getTaskStatus() < 4){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "只有已完成和取消的任务可以关闭");
				return infoMap;
			}
			
			if(taskInfo.getUserId().equals(userId)){
				if(taskInfo.getTaskStatus() == 7){
					taskInfo.setTaskStatus(8);
				}else if(taskInfo.getTaskStatus() == 4 || taskInfo.getTaskStatus() == 5){
					taskInfo.setTaskStatus(6);
				}
			}else{
				if(taskInfo.getTaskStatus() == 6){
					taskInfo.setTaskStatus(8);
				}else if(taskInfo.getTaskStatus() == 4 || taskInfo.getTaskStatus() == 5){
					taskInfo.setTaskStatus(7);
				}
			}
			taskInfoMapper.updateByPrimaryKeySelective(taskInfo);
			
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", "成功");
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}

	/**
	 * 我的（他人）任务接口
	 * 
	 * @param request
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/getTaskInfoByUserId")
	@ResponseBody
	public Object getTaskInfoByUserId(HttpServletRequest request, TaskInfo taskInfo,
			@RequestParam(value="pageNow",defaultValue="1",required=false) Integer page,
			@RequestParam(value="pageSize",required=false,defaultValue="10") Integer rows
			) {
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		try {
			boolean isOther = false;
			
			String userId = taskInfo.getUserId();
			JSONObject obj = PeiniUtils.validToken(taskInfo.getUserId());
			if(obj.getInteger("resultCode") != 1){
				//return obj;
				isOther = true;
			}else{
				userId = obj.getString("userId");
				taskInfo.setUserId(userId);
			}
			
			PageHelper.startPage(page, rows);
			List<LinkedHashMap<String, Object>> taskInfoByUserIdList = taskInfoService.getTaskInfoByUserId(taskInfo);
			for (LinkedHashMap<String, Object> map : taskInfoByUserIdList) {
				Integer taskId = Integer.parseInt(map.get("id").toString());
				Integer sellerInfoId = Integer.parseInt(map.get("sellerInfoId").toString());
				map.put("nickName", PeiniUtils.updateNickName(map.get("nickName").toString(),map.get("accCode").toString()));
				if(isOther == true){//查看他人任务
					if(map.get("taskStatus").toString().equals("1")){//发布中的任务
						Date taskAppointedTime = DateTimeUtils.formatString2Date(map.get("taskAppointedTime").toString(), "yyyy-MM-dd HH:mm:ss");
						if(taskAppointedTime.before(new Date())){
							map.put("taskStatus", "5");
							
							TaskInfo info = new TaskInfo();
							info.setId(taskId);
							info.setTaskStatus(5);
							info.setTaskCancleType(3);
							info.setTaskCancleTime(new Date());
							info.setCancelUserId(map.get("userId").toString());
							taskInfoMapper.updateByPrimaryKeySelective(info);
						}
					}
				}
				
				if(map.get("otherUserId") != null){
					String otherUserId = map.get("otherUserId").toString();
					if(StringUtils.isNotBlank(otherUserId)){
						UserInfo otherUser = userInfoMapper.selectByPrimaryKey(otherUserId);
						if(otherUser == null){
							map.put("otherPhone", "");
							map.put("otherNickName", "");
						}else{
							map.put("otherPhone", otherUser.getUserLogin().getUserName());
							map.put("otherNickName", PeiniUtils.updateNickName(otherUser.getNickname(),otherUser.getAccCode()));
						}
					}else{
						map.put("otherPhone", "");
						map.put("otherNickName", "");
					}
					
				}else{
					map.put("otherPhone", "");
					map.put("otherNickName", "");
				}
				
				SystemSeller systemSeller = PeiniUtils.yetaiMap.get(map.get("sellerBigType").toString());
				String sellerBigName = systemSeller.getCodesName();
				String imgSrc = systemSeller.getImageSrc();
				map.put("sellerBigName", sellerBigName);
				map.put("sellerTypeImg", imgSrc);
			}
			
			List<Integer> statusList = new ArrayList<Integer>();
			
			if(taskInfo.getSort() == null){
				TaskInfoExample example = new TaskInfoExample();
				com.jsz.peini.bean.task.TaskInfoExample.Criteria criteria = example.createCriteria();
				statusList.add(1);
				statusList.add(2);
				statusList.add(3);
				statusList.add(4);
				statusList.add(5);
				statusList.add(7);
				criteria.andTaskStatusIn(statusList);
				criteria.andUserIdEqualTo(userId);
				
				statusList = new ArrayList<Integer>();
				com.jsz.peini.bean.task.TaskInfoExample.Criteria criteria2 = example.createCriteria();
				statusList.add(21);
				statusList.add(31);
				statusList.add(41);
				statusList.add(5);
				statusList.add(6);
				criteria2.andTaskOtherStatusIn(statusList);
				criteria2.andOtherUserIdEqualTo(userId);
				example.or(criteria2);
				
				Long finishCnt = taskInfoMapper.countByExample(example);
				
				example = new TaskInfoExample();
				criteria = example.createCriteria();
				criteria.andTaskCancleTypeNotEqualTo(3);//系统取消
				criteria.andTaskCancleTypeNotEqualTo(2);//双方原因取消
				criteria.andCancelUserIdEqualTo(userId);
				
				Long cancleCnt = taskInfoMapper.countByExample(example);
				
				HashMap<String, Object> finishMap = new HashMap<String, Object>();
				Long totalCnt = finishCnt;
				finishMap.put("regCnt", totalCnt);
				if(totalCnt == 0){
					finishMap.put("finishRatio", 0);
				}else{
					long num = (long)(((double)finishCnt-cancleCnt)/(finishCnt)*100);
					if(num == 0){
						finishMap.put("finishRatio", "0");
					}else{
						finishMap.put("finishRatio", num);
					}
				}
				infoMap.put("completeness", finishMap);
			}else if(taskInfo.getSort() == 1){//我发的
				TaskInfoExample example = new TaskInfoExample();
				statusList = new ArrayList<Integer>();
				com.jsz.peini.bean.task.TaskInfoExample.Criteria criteria = example.createCriteria();
				statusList.add(1);
				statusList.add(2);
				statusList.add(3);
				statusList.add(4);
				statusList.add(5);
				statusList.add(7);
				criteria.andUserIdEqualTo(userId);
				
				Long finishCnt = taskInfoMapper.countByExample(example);
				
				example = new TaskInfoExample();
				criteria = example.createCriteria();
				criteria.andUserIdEqualTo(userId);
				criteria.andTaskCancleTypeNotEqualTo(3);//系统取消
				criteria.andTaskCancleTypeNotEqualTo(2);//双方原因取消
				criteria.andCancelUserIdEqualTo(userId);
				
				Long cancleCnt = taskInfoMapper.countByExample(example);
				
				HashMap<String, Object> finishMap = new HashMap<String, Object>();
				Long totalCnt = finishCnt;
				finishMap.put("regCnt", totalCnt);
				if(totalCnt == 0){
					finishMap.put("finishRatio", 0);
				}else{
					long num = (long)(((double)finishCnt-cancleCnt)/(finishCnt)*100);
					if(num == 0){
						finishMap.put("finishRatio", "0");
					}else{
						finishMap.put("finishRatio", num);
					}
				}
				infoMap.put("completeness", finishMap);
			}else if(taskInfo.getSort() == 2){//我参与的
				TaskInfoExample example = new TaskInfoExample();
				statusList = new ArrayList<Integer>();
				com.jsz.peini.bean.task.TaskInfoExample.Criteria criteria = example.createCriteria();
				statusList.add(21);
				statusList.add(31);
				statusList.add(41);
				statusList.add(5);
				statusList.add(6);
				criteria.andOtherUserIdEqualTo(userId);
				
				Long finishCnt = taskInfoMapper.countByExample(example);
				
				
				example = new TaskInfoExample();
				criteria = example.createCriteria();
				criteria.andOtherUserIdEqualTo(userId);
				criteria.andTaskCancleTypeNotEqualTo(3);//系统取消
				criteria.andTaskCancleTypeNotEqualTo(2);//双方原因取消
				criteria.andCancelUserIdEqualTo(userId);
				
				Long cancleCnt = taskInfoMapper.countByExample(example);
				
				HashMap<String, Object> finishMap = new HashMap<String, Object>();
				Long totalCnt = finishCnt;
				finishMap.put("regCnt", totalCnt);
				if(totalCnt == 0){
					finishMap.put("finishRatio", 0);
				}else{
					long num = (long)(((double)finishCnt-cancleCnt)/(finishCnt)*100);
					if(num == 0){
						finishMap.put("finishRatio", "0");
					}else{
						finishMap.put("finishRatio", num);
					}
				}
				infoMap.put("completeness", finishMap);
			}
			
			infoMap.put("taskInfoByUserIdList", taskInfoByUserIdList);
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", "成功");
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.FAIL);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		//logger.info(net.sf.json.JSONObject.fromObject(infoMap));
		return infoMap;
	}

	/**
	 * 发起取消任务申请
	 * 
	 * @param request
	 * @param taskInfo
	 * @return
	 */
	@RequestMapping(value = "/cancelTaskInfo")
	@ResponseBody
	public Object cancelTaskInfo(HttpServletRequest request, 
			@RequestParam(value="taskId") Integer taskId,
			@RequestParam(value="userId") String userId,
			@RequestParam(value="cancleType",required=false) Integer cancleType //1个人；2双方
			) {
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		try {
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			userId = obj.getString("userId");	
			
			TaskInfo taskInfo = taskInfoMapper.selectByPrimaryKey(taskId);
			
			if(taskInfo.getTaskStatus() >= 2){
				if(!taskInfo.getUserId().equals(userId) && !taskInfo.getOtherUserId().equals(userId)){
					infoMap.put("resultCode", Constants.EXCEPTION);
					infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
					return infoMap;
				}else{
					if(taskInfo.getTaskStatus() >= 3 || taskInfo.getTaskOtherStatus() >= 31){
						infoMap.put("resultCode", Constants.FAIL);
						infoMap.put("resultDesc", "付款后的任务不可以取消");
						return infoMap;
					}
				}
			}else {
				if(!taskInfo.getUserId().equals(userId)){
					infoMap.put("resultCode", Constants.EXCEPTION);
					infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
					return infoMap;
				}
			}
			infoMap=taskInfoService.cancelTaskInfo(taskInfo,userId,taskId,cancleType);
			return infoMap;
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	/**
	 * 同意或者拒绝取消任务接口
	 * @param request
	 * @param taskId
	 * @param userId
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/cancelTaskInfo_result")
	@ResponseBody
	public Object cancelTaskInfo_refuse(HttpServletRequest request, 
			@RequestParam(value="taskId") Integer taskId,
			@RequestParam(value="userId") String userId,
			@RequestParam(value="type") Integer type  //1同意；0拒绝
			) {
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		try {
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			userId = obj.getString("userId");
			
			TaskInfo taskInfo = taskInfoMapper.selectByPrimaryKey(taskId);
			infoMap=taskInfoService.cancelTaskInfo_refuse(taskInfo,userId,taskId,type,null);
		}catch(Exception e){
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	/**
	 * 聊天界面任务信息
	 * @author Han Guohong
	 * @param request
	 * @param userId
	 * @param nameStr
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getTaskInfoByPhones")
	public Object getTaskInfoByPhones(HttpServletRequest request, 
			@RequestParam(value="userId") String userId,
			@RequestParam(value="nameStr") String nameStr,
			@RequestParam(value="xpoint") String xpoint,
			@RequestParam(value="ypoint") String ypoint
			) {
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		try {
			
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			userId = obj.getString("userId");
			
			if(StringUtils.isBlank(nameStr)){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "参数不能为空");
				return infoMap;
			}
			
			int n = 0;
			String [] ss = nameStr.split(",");
			List<String> nameList = Arrays.asList(ss);
			for (int i = 0; i < nameList.size(); i++) {
				if(nameList.get(i).equals("admin")){
					n = i;
				}
			}
			
			UserInfoExample userInfoExample = new UserInfoExample();
			userInfoExample.createCriteria().andUserPhoneIn(nameList);
			List<UserInfo> list = userInfoMapper.selectByExample(userInfoExample);
			if(list.size() != 2){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "没有查到相关任务信息");
				return infoMap;
			}
			
			String otherId = "";
			for (UserInfo userInfo : list) {
				if(!userInfo.getId().equals(userId)){
					otherId = userInfo.getId();
				}
			}
			
			TaskInfoExample example = new TaskInfoExample();
			com.jsz.peini.bean.task.TaskInfoExample.Criteria criteria = example.createCriteria();
			example.or().andUserIdEqualTo(userId).andOtherUserIdEqualTo(otherId);
			example.or().andOtherUserIdEqualTo(userId).andUserIdEqualTo(otherId);
			example.setOrderByClause("task_recv_time desc");
			List<TaskInfo> taskInfos = taskInfoMapper.selectByExample(example);
			if(taskInfos.size() == 0){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "没有查到相关任务信息");
				return infoMap;
			}
			TaskInfo taskInfo = taskInfos.get(0);
			HashMap<String, Object> taskMap = new HashMap<String, Object>();
			
			UserInfo otherInfo  = userInfoMapper.selectByPrimaryKey(otherId);
			taskMap.put("taskId", taskInfo.getId().toString());
			taskMap.put("headImg", StringUtils.trimToEmpty(otherInfo.getImageHead()));
			taskMap.put("nickName", StringUtils.trimToEmpty(PeiniUtils.updateNickName(otherInfo.getNickname(),otherInfo.getAccCode())));
			if(otherInfo.getIndustry() == null || otherInfo.getIndustry() == 0){
				taskMap.put("industry","");
			}else{
				taskMap.put("industry", otherInfo.getIndustry() == null ? "" : PeiniUtils.industryMap.get(otherInfo.getIndustry()).getCodesName());
			}
			taskMap.put("taskTime", DateTimeUtils.formatDateTime2String(taskInfo.getTaskAppointedTime(), "yyyy-MM-dd hh:mm"));
			taskMap.put("sellerName", taskInfo.getSellerInfoName());
			taskMap.put("targetDesc", StringUtils.trimToEmpty(PeiniUtils.yetaiMap.get(taskInfo.getSellerSmallType().toString()).getCodesContent()));
			taskMap.put("taskAppointedTime", DateTimeUtils.formatDateTime2String(taskInfo.getTaskAppointedTime(), "yyyy-MM-dd HH:mm"));
			taskMap.put("otherBuy", taskInfo.getOtherBuy().toString());
			taskMap.put("otherId", otherId);
			
			Integer distance = CommonUtils.contrastMap(xpoint, ypoint,(String) taskInfo.getXpoint(), (String) taskInfo.getYpoint());
			taskMap.put("distance", distance == null ? 0 : distance);
			
			if(userId.equals(taskInfo.getUserId())){//我发布的
				taskMap.put("publishType", "1");
			}else{//他人发布的
				taskMap.put("publishType", 0);
			}
			taskMap.put("status", taskInfo.getTaskStatus().toString());
			taskMap.put("taskOtherStatus", taskInfo.getTaskOtherStatus() == null ? "" : taskInfo.getTaskOtherStatus().toString());
			
			infoMap.put("data", taskMap);
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", "成功");
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	@ApiOperation("地图上显示的任务列表")
	@RequestMapping(value = "/task/mapSearch", method=RequestMethod.POST, consumes="application/json")
	@ResponseBody
	public Object searchTaskInMap(@RequestBody @Valid TaskInMapSearchVo vo, BindingResult binding) {
		if(binding.hasErrors()){
			return ResultMessage.FAIL.setBindingResult(binding);
		}
		vo.setXpoint(ThreadUtil.getThreadInfo().getXpoint());
		vo.setYpoint(ThreadUtil.getThreadInfo().getYpoint());
		if(vo.getXpoint() == null || vo.getYpoint() == null){
			return ResultMessage.FAIL.setMessage("未获取到您的定位");
		}
		List<CommonTaskInfo> taskList = taskInfoService.searchTasks(vo);
		List<MergedCommonTaskInfo> mergedTaskList = new ArrayList<MergedCommonTaskInfo>();
		for(int i=0; i< taskList.size();i++){
			MergedCommonTaskInfo merge = new MergedCommonTaskInfo();
			CommonTaskInfo taskI = taskList.get(i);
			if(taskI.isMerged()){
				continue;
			}
			merge.add(taskI);
			taskI.setMerged(true);
			for(int j=i+1;j< taskList.size(); j++){
				CommonTaskInfo taskJ = taskList.get(j);
				if(taskJ.isMerged()){
					continue;
				}
				if(CommonUtils.contrastMap(taskI.getXpoint(), taskI.getYpoint(), taskJ.getXpoint(), taskJ.getYpoint()) < vo.getScale()*1.5){
					merge.add(taskJ);
					taskJ.setMerged(true);
				}
			}
			mergedTaskList.add(merge);
		}
		return ResultMessage.SUCCESS.setData(mergedTaskList);
	}
	
	
	@ApiOperation("我的任务")
	@RequestMapping(value = "/mytask", method=RequestMethod.GET)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "type", paramType="query", value = "类型: all, my, other", required = true, dataType = "String"),
		@ApiImplicitParam(name = "status", paramType="query", value = "状态: all, cancel, finish, ongoing, publish", required = true, dataType = "String"),
		@ApiImplicitParam(name = "pageNum", paramType="query", required = true),
		@ApiImplicitParam(name = "pageSize", paramType="query", required = true)
	})
	@ResponseBody
	public Object getMyTasks(@RequestParam("type") String type,@RequestParam("status") String status,@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
		String userId = ThreadUtil.getThreadInfo().getUserId();
		if(userId == null){
			return ResultMessage.AUTH_FAILED;
		}
		List<Map<String,Object>> taskList = taskInfoService.getUserTaskInfoList(userId, type, status);
		ResultMessage resultMsg = ResultMessage.SUCCESS;
		int fromIndex = (pageNum-1) * pageSize;
		int toIndex = pageNum * pageSize;
		if(fromIndex >= taskList.size()){
			resultMsg.setData(null);
		}else if(toIndex >= taskList.size()){
			resultMsg.setData(taskList.subList(fromIndex, taskList.size()));
		}else{
			resultMsg.setData(taskList.subList(fromIndex,toIndex));
		}
		return resultMsg;
	}
	
	
	@ApiOperation("多任务列表")
	@RequestMapping(value = "/multiTasks", method=RequestMethod.GET)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "tasks", paramType="query", required = true, dataType = "String"),
	})
	@ResponseBody
	public Object getMultiTasks(@RequestParam("tasks") String tasks) {
		String userId = ThreadUtil.getThreadInfo().getUserId();
		if(userId == null){
			return ResultMessage.AUTH_FAILED;
		}
		
		JSONObject json = (JSONObject)JSON.parse(tasks);
		String taskIds = json.getString("taskIds") + "-1";
		String partyIds = json.getString("partyIds") + "-1";
		
		List<Map<String,Object>> taskList = taskInfoService.getMultiTaskList(taskIds, partyIds);
		return ResultMessage.SUCCESS.setData(taskList);
	}
}
